package cn.zbh.pet.controller;


import cn.zbh.basic.annotation.PreAuthorize;
import cn.zbh.basic.util.AjaxResult;

import cn.zbh.basic.util.PageList;
import cn.zbh.pet.domain.PetType;
import cn.zbh.pet.query.PetTypeQuery;
import cn.zbh.pet.service.IPetTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// @Controller
// @ResponseBody
@RestController // controller层，返回json格式数据
@RequestMapping("/petType")
@Api(value = "宠物类型的API",description="宠物类型相关的CRUD功能")
public class PetTypeTypeController {

    @Autowired
    private IPetTypeService petTypeService;

    /**
     * http://localhost:8080/PetType  url
     * 动词： get
     * @return
     */
    // @RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping // 上面的结合
    @ApiOperation(value = "查询所有宠物类型信息" )
    public List<PetType> findAll(){

        return petTypeService.findAll();
    }

    /**
     * 查询一个  接收参数id
     *   url: http://localhost:8080/PetType/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    public PetType findById(@PathVariable("id") Long id){
        return petTypeService.findById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     *   JsonResult ：链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除" )
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            petTypeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * http://localhost:8080/PetType
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或者修改" )
    public AjaxResult updateOrAdd(@RequestBody PetType PetType){

        try {
            if(PetType.getId()==null){
                // 添加操作
                petTypeService.add(PetType);
            }else{
                // 修改操作
                petTypeService.update(PetType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败");
        }
    }




    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    public AjaxResult patchDel(@RequestBody List<Long> ids){

        try {
            petTypeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络延迟，稍后重试");
        }
    }


    /**
     *  分页数据
     * @return
     */
    @ApiOperation(value = "分页查询接口" )
    @PostMapping
    public PageList<PetType> queryPage(@RequestBody PetTypeQuery query){
        try {
            // 哈哈哈
            return petTypeService.query(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<PetType>();
        }
    }
    @GetMapping("/typeTree")
    public List<PetType> typeTree(){
        return petTypeService.typeTree();
    }

}
